<?php

class UrlGenController extends Zend_Controller_Action
{

    public function init()
    {
    	
    }

    public function indexAction()
    {
        $this->view->title = 'Generate View Once Url';
    }

    public function historyAction()
    {
    	$this->view->title = 'Generated URLs History';
    	
    	$uniqueTokensDbTable = new Application_Model_DbTable_UniqueTokens();
    	$uniqueTokens = $uniqueTokensDbTable->fetchAll('send_to_email is not null', 'date_updated DESC');
    	
    	$this->view->uniqueTokens = $uniqueTokens;
    }

    public function generateAction()
    {
        $uniqueTokensDbTable = new Application_Model_DbTable_UniqueTokens();
        $form = new Application_Form_SendLink();
        $this->view->title = 'Send link to customer';
        $this->view->form = $form;
        $this->view->formResponse = '';
        
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->getRequest()->getPost();
    		
    		if (isset($_POST['send_link'])) {
	    		if ($form->isValid($formData)) {
	    			
		    			$id = (int) $form->getValue('id');
		    			$sendToEmail = $form->getValue('send_to_email');
		        		
		        		$uniqueTokensDbTable->updateUniqueToken($id, array('send_to_email' => $sendToEmail, 'is_active' => 'Y'));
		        		
		        		$uniqueToken = $uniqueTokensDbTable->getUniqueToken($id);
		        		$this->view->uniqueToken = $uniqueToken;
		        		
		        		$emailUtil = new ViewOnceUrlGen_Util_Email();
		        		$emailUtil->sendLinkEmail($uniqueToken);
		        		
		        		$this->forward('email-sent', 'url-gen', null, $uniqueToken);
	    		} else {
	    			$this->view->formResponse = 'Sorry, there was a problem with your submission. Please check the following:';
	    			$form->populate($formData);
	    		}
    		} else {
    			$id = $formData['id'];
    			$uniqueTokensDbTable->deleteUniqueToken($id);
    			$this->_helper->redirector('index');
    		}
    	} else {
    		$token = $this->_generateUniqueToken();
    		$url = $this->_generateViewOnceUrl($token);
    		$username = $this->_getCurrentUsername();
    		
    		$id = $uniqueTokensDbTable->addUniqueToken($token, $url, $username);
    		
    		if ($id > 0) {
    			$uniqueToken = $uniqueTokensDbTable->getUniqueToken($id);
    			$form->populate($uniqueToken);
    		}
    	}
    }

    protected function _generateUniqueToken()
    {
    	return md5(uniqid(rand(), 1));
    }

    protected function _generateViewOnceUrl($token)
    {
    	$config = Zend_Registry::get('config');
    	$domain = ViewOnceUrlGen_Util_Url::_siteURL();
    	$orderFormPage = $config->url->order->form->page;
    	return $domain . $orderFormPage . $token;
    }

    protected function _getCurrentUsername()
    {
    	$auth = Zend_Auth::getInstance();
    	if ($auth->hasIdentity()) {
    		return $auth->getIdentity()->username;
    	} else {
    		return 0;
    	}
    }

    public function emailSentAction()
    {
    	$this->view->title = 'Link sent to customer';
    }
}









